'use strict';

const { Controller } = require('egg');

class UserController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = {
      code: 0,
      result: ctx.request.body,
    };
  }

  // 自由传参模式 /ming/geta?name=mn&age=99
  async getA() {
    const { ctx } = this;
    ctx.body = {
      code: 0,
      result: ctx.query,
    };
  }

  // 严格传参模式 /ming/getb/mn/99
  async getB() {
    const { ctx } = this;
    ctx.body = {
      code: 0,
      result: ctx.params,
    };
  }

  // 增，删，改，查

  async addGirl() {
    const { ctx } = this;
    const res = await ctx.service.ming.addGirl(ctx.request.body);
    if (res) {
      ctx.body = {
        code: 0,
        message: '数据添加成功',
        result: { id: res },
      };
    } else {
      ctx.body = {
        code: 0,
        result: '数据添加失败',
      };
    }
  }

  async deleteGirl() {
    const { ctx } = this;
    const res = await ctx.service.ming.deleteGirl(ctx.params.id);
    if (res) {
      ctx.body = {
        code: 0,
        result: '数据删除成功',
      };
    }
  }

  async updateGirl() {
    const { ctx } = this;
    const res = await ctx.service.ming.updateGirl(ctx.request.body);
    if (res) {
      ctx.body = {
        code: 0,
        result: '数据更新成功',
      };
    }
  }

  // /ming/girl?id=1
  async getGirl() {
    const { ctx } = this;
    const res = await ctx.service.ming.getGirl(ctx.query.id);
    ctx.body = {
      code: 0,
      result: res,
    };
  }
}

module.exports = UserController;
